package com.tsd.system.controller;

import com.alibaba.fastjson.JSON;
import com.tsd.CommonMsgNameConstants;
import com.tsd.CommonParamNameConstants;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.ErrorCode;
import com.tsd.core.vo.HttpResult;
import com.tsd.core.vo.PageBean;
import com.tsd.system.entity.SysGroupRole;
import com.tsd.system.entity.SysUser;
import com.tsd.system.service.SysGroupRoleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 描述：SysGroupRole 控制层
 *
 * @author Hillpool
 * @date 2019/03/12 12:20:51
 */
@Controller
@Scope("prototype")
@RequestMapping("/api/v2")
public class SysGroupRoleController extends SystemBaseController {

    private final SysGroupRoleService sysGroupRoleService;

    @Autowired
    public SysGroupRoleController(SysGroupRoleService sysGroupRoleService) {
        this.sysGroupRoleService = sysGroupRoleService;
    }

    @ApiOperation(value = "根据sid获取SysGroupRole")
    @ResponseBody
    @RequestMapping(value = "/getSysGroupRoleBySid.action", method = RequestMethod.POST)
    public HttpResult<Object> getSysGroupRoleBySid() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            Long id = (Long) map.get("id");
            super.checkEmpty(id, "请提供参数id");
            SysGroupRole sysGroupRole = sysGroupRoleService.getSysGroupRoleById(id);
            hr.setData(sysGroupRole);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "保存SysGroupRole")
    @ResponseBody
    @RequestMapping(value = "/saveSysGroupRole.action", method = RequestMethod.POST)
    public HttpResult<Object> saveSysGroupRole() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            SysUser sysUser = this.getSysUser();
            Map<String, Object> map = super.getParams(request);
            String json = JSON.toJSONString(map);
            SysGroupRole t = JSON.parseObject(json, SysGroupRole.class);
            sysGroupRoleService.saveSysGroupRole(t, sysUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "根据id删除SysGroupRole")
    @ResponseBody
    @RequestMapping(value = "/deleteSysGroupRole.action", method = RequestMethod.POST)
    public HttpResult<Object> deleteSysGroupRole() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            Long id = (Long) map.get("id");
            super.checkEmpty(id, "请提供参数id");
            sysGroupRoleService.deleteSysGroupRole(id);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "设置角色拥有的菜单列表", notes = " 以覆盖的方式设置角色的菜单列表")
    @ResponseBody
    @RequestMapping(value = "/saveRoleList2Group.action", method = RequestMethod.POST)
    public HttpResult<Object> saveRoleList2Group() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            SysUser opUser = this.getSysUser();
            String groupSid = HlpUtils.getString(map, CommonParamNameConstants.GROUP_SID);
            String json = JSON.toJSONString(map.get(CommonParamNameConstants.ROLE_ID_LIST));
            List<String> roleIdList = JSON.parseArray(json, String.class);
            sysGroupRoleService.saveSysRoleList2Group(opUser, groupSid, roleIdList);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "根据部门sid获取角色")
    @ResponseBody
    @RequestMapping(value = "/findGroupRolesByGroupSid.action", method = RequestMethod.POST)
    public HttpResult<Object> findGroupRolesByGroupSid() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            String groupSid = HlpUtils.getString(map, CommonParamNameConstants.GROUP_SID);
            super.checkEmpty(groupSid, CommonMsgNameConstants.getEmptyMsg(CommonParamNameConstants.GROUP_SID));
            PageBean page = new PageBean(map);
            List<SysGroupRole> list = sysGroupRoleService.findSysGroupRolesByGroupSid(groupSid);
            hr.setData(list);
            hr.setPage(page);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }
}
